package cn.edu.zucc.service.impl;

import cn.edu.zucc.entity.TArticle;
import cn.edu.zucc.mapper.TArticleMapper;
import cn.edu.zucc.service.TArticleService;
import org.springframework.stereotype.Service;
import top.starp.util.ReturnT;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @description t_article
 * @author zhengkai.blog.csdn.net
 * @date 2022-04-17
 */
@Service
public class TArticleServiceImpl implements TArticleService {

	@Resource
	private TArticleMapper tArticleMapper;


	@Override
	public Object insert(TArticle tArticle) {

		// valid
		if (tArticle == null) {
			return ReturnT.error("必要参数缺失");
        }

		tArticleMapper.insert(tArticle);
        return ReturnT.success();
	}


	@Override
	public Object delete(Integer id) {
		int ret = tArticleMapper.delete(id);
		return ret>0?ReturnT.success():ReturnT.error();
	}


	@Override
	public Object update(TArticle tArticle) {
		int ret = tArticleMapper.update(tArticle);
		return ret>0?ReturnT.success():ReturnT.error();
	}


	@Override
	public TArticle getById(Integer id) {
		return tArticleMapper.getById(id);
	}

    @Override
    public Object save(TArticle tArticle) {
        Integer id = tArticle.getId();
        if (id==null) {
            int insert = tArticleMapper.insert(tArticle);
            return ReturnT.success();
        }
        TArticle load = tArticleMapper.getById(tArticle.getId());
        if (load==null) {
            int insert = tArticleMapper.insert(tArticle);

            return ReturnT.success();
        }
        tArticleMapper.update(tArticle);
        return  ReturnT.success();
    }


	@Override
	public Map<String,Object> pageList(int offset, int pageSize) {

		List<TArticle> pageList = tArticleMapper.pageList(offset, pageSize);
        Integer totalCount = tArticleMapper.pageListCount(offset, pageSize);

		// result
		Map<String, Object> result = new HashMap<String, Object>();
		//result.put("pageList", pageList);
		result.put("content", pageList);
		result.put("totalCount", totalCount);

		return result;
	}

}
